package org.persistent.impl;

import org.persistent.CheckUsernameDao;
import org.persistent.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class CheckUsernameDaoImpl implements CheckUsernameDao {
    private static final String GET_USERNAME_LIST = "SELECT userid FROM account";

    @Override
    public boolean usernameIsExist(String username) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_USERNAME_LIST);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                String existUsername = resultSet.getString(1);
                if(existUsername.equals(username)){
                    return true;
                }
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
